﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

[Serializable]
public partial class admin_listHocVien : System.Web.UI.UserControl
{
    #region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    private int intUserList = 0;
    int intNhom = 0;
    int intChucNangID = 0;
    int intLevelID = 0;
    bool boolLevel2345678 = false;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;    
    DALevel objLevel;
    DAQuyen objQuyen;
    DAHocVienLV2 objHocVienLV2;
    DAHocVienLV3LV5 objHocVienLV3LV5;
    DAHocVienLV6LV8 objHocVienLV6LV8;
    DAHocVien objHocVien;
    DANguoidung objNguoidung;
    DATrangThai objTrangThai;
    DATrangThaiGoi objTrangThaiGoi;
    DALog objLog;
    DATruong objTruong;
    #endregion

    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion

    #region "Methods methods"
    // Hien thi Level tren TreeView
    private void loadLevel()
    {
        try
        {
            this.tvLevel.Nodes.Clear();
            DataSet dsLevel = objNguoidung.loadNguoiDungLevel(intUserID);
            if (dsLevel != null && dsLevel.Tables[0].Rows.Count > 0)
            {
                string strFullName = "";
                try
                {
                    DataRow drNguoidung = objNguoidung.loadNguoidungID(intUserID);
                    strFullName = drNguoidung["HoTen"].ToString();
                }
                catch { }
                int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intUserID, 0);
                ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                rootNode.Text = strFullName + " (" + intCount.ToString() + ")";
                rootNode.ImageUrl = "user.gif";
                rootNode.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=0&UserList=" + intUserID.ToString();
                //rootNode.ID = intUserID.ToString();
                if (intUserList == intUserID && intNhom.ToString().Equals("0"))
                {
                    rootNode.Expanded = true;
                }
                
                //BuildTreeSub(dsLevel.Tables[0].Rows, rootNode, intUserID);
                foreach (DataRow dr in dsLevel.Tables[0].Rows)
                {
                    int LevelID = Convert.ToInt32(dr["ID"].ToString());
                    if (LevelID >= 2)
                    {
                        boolLevel2345678 = true;
                        DataSet drQUyenTemp = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
                        foreach (DataRow drTemp in drQUyenTemp.Tables[0].Rows)
                        {
                            int intQuyenTemp = Convert.ToInt32(drTemp["QuyenID"].ToString());
                            if (intQuyenTemp == 3 && intLevelID == 2)
                            {
                                boolLevel2345678 = false;
                                break;
                            }
                        }
                    }
                    intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intUserID, LevelID);
                    ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
                    node.Text = dr["Ten"].ToString() + " (" + intCount.ToString() + ")";
                    node.ImageUrl = "file.gif";
                    //if (intLevelID == LevelID && intUserList == intUserID)
                    //{
                    //    node.CssClass = "SelectedTreeNode";
                    //}
                    node.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=0&LevelView=" + dr["ID"].ToString() + "&UserList=" + intUserID.ToString();
                    //node.ID = dr["Ma"].ToString();
                    rootNode.Nodes.Add(node);
                }
                this.tvLevel.Nodes.Add(rootNode);
            }

            if (intUserID == 1)
            {
                DataSet dsQuyen = objQuyen.loadQuyen();
                foreach (DataRow drQuyen in dsQuyen.Tables[0].Rows)
                {
                    ComponentArt.Web.UI.TreeViewNode rootNodeQuyen = new ComponentArt.Web.UI.TreeViewNode();
                    rootNodeQuyen.Text = drQuyen["Tennhom"].ToString();
                    rootNodeQuyen.ImageUrl = "folder.gif";
                    //rootNodeQuyen.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                    //rootNodeQuyen.ID = drQuyen["ID"].ToString();
                    //if (intNhom.ToString() == drQuyen["ID"].ToString())
                    //{
                    //    rootNodeQuyen.Expanded = true;
                    //}
                    this.tvLevel.Nodes.Add(rootNodeQuyen);
                    DataSet dsNguoiDungCapDuoi = objNguoidung.loadQuyenNguoiDungByQuyen(Convert.ToInt32(drQuyen["ID"].ToString()),intUserID);
                    DataRow[] drNguoiDungCapDuoiV = dsNguoiDungCapDuoi.Tables[0].Select("ID <> 1");
                    int i = 1;
                    foreach (DataRow dr in drNguoiDungCapDuoiV)
                    {
                        int intNguoiDungCapDuoi = Convert.ToInt32(dr["ID"].ToString());
                        int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDungCapDuoi, 0);
                        ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                        rootNode.Text = dr["HoTen"].ToString() + " (" + intCount.ToString() + ")";
                        rootNode.ImageUrl = "user.gif";
                        rootNode.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyen["ID"].ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                        //rootNode.ID = intNguoiDungCapDuoi.ToString();
                        rootNodeQuyen.Nodes.Add(rootNode);
                        //if (intUserList == intNguoiDungCapDuoi)
                        //{
                        //    rootNode.Expanded = true;
                        //}
                        i++;
                        DataSet dsNguoiDungCapDuoiLevel = objNguoidung.loadNguoiDungLevel(intNguoiDungCapDuoi);
                        if (dsNguoiDungCapDuoiLevel != null && dsNguoiDungCapDuoiLevel.Tables[0].Rows.Count > 0)
                        {
                            BuildTreeSub(dsNguoiDungCapDuoiLevel.Tables[0].Rows, rootNode, intNguoiDungCapDuoi, Convert.ToInt32(drQuyen["ID"].ToString()));
                        }
                    }
                }
            }
            else
            {
                bool checkAdmin = false;
                DataSet dsQuyenNguoidung = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
                if (dsQuyenNguoidung != null && dsQuyenNguoidung.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in dsQuyenNguoidung.Tables[0].Rows)
                    {
                        if (dr["QuyenID"].ToString().Trim().Equals("1") || dr["QuyenID"].ToString().Trim().Equals("1000"))
                        {
                            checkAdmin = true;
                            break;
                        }
                    }
                }
                DataRowCollection drNguoiDungCapDuois;
                if (checkAdmin)
                {
                    DataSet dsQuyen = objQuyen.loadQuyen();
                    foreach (DataRow drQuyen in dsQuyen.Tables[0].Rows)
                    {
                        ComponentArt.Web.UI.TreeViewNode rootNodeQuyen = new ComponentArt.Web.UI.TreeViewNode();
                        rootNodeQuyen.Text = drQuyen["Tennhom"].ToString();
                        rootNodeQuyen.ImageUrl = "folder.gif";
                        //rootNodeQuyen.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                        //rootNodeQuyen.ID = drQuyen["ID"].ToString();
                        //if (intNhom.ToString() == drQuyen["ID"].ToString())
                        //{
                        //    rootNodeQuyen.Expanded = true;
                        //}
                        this.tvLevel.Nodes.Add(rootNodeQuyen);
                        DataSet dsNguoiDungCapDuoi = objNguoidung.loadQuyenNguoiDungByQuyen(Convert.ToInt32(drQuyen["ID"].ToString()),intUserID);
                        DataRow[] drNguoiDungCapDuoiV = dsNguoiDungCapDuoi.Tables[0].Select("ID <> 1");
                        int i = 1;
                        foreach (DataRow dr in drNguoiDungCapDuoiV)
                        {
                            int intNguoiDungCapDuoi = Convert.ToInt32(dr["ID"].ToString());
                            int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDungCapDuoi, 0);
                            ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                            rootNode.Text = dr["HoTen"].ToString() + " (" + intCount.ToString() + ")";
                            rootNode.ImageUrl = "user.gif";
                            rootNode.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyen["ID"].ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                            //rootNode.ID = intNguoiDungCapDuoi.ToString();
                            rootNodeQuyen.Nodes.Add(rootNode);
                            //if (intUserList == intNguoiDungCapDuoi)
                            //{
                            //    rootNode.Expanded = true;
                            //}
                            i++;
                            DataSet dsNguoiDungCapDuoiLevel = objNguoidung.loadNguoiDungLevel(intNguoiDungCapDuoi);
                            if (dsNguoiDungCapDuoiLevel != null && dsNguoiDungCapDuoiLevel.Tables[0].Rows.Count > 0)
                            {
                                BuildTreeSub(dsNguoiDungCapDuoiLevel.Tables[0].Rows, rootNode, intNguoiDungCapDuoi, Convert.ToInt32(drQuyen["ID"].ToString()));
                            }
                        }
                    }


                    //DataSet dsQuyen = objQuyen.loadQuyen();
                    //DataSet dsNguoiDungCapDuoi = objNguoidung.loadQuyenNguoiDungCapDuoiByNguoiDungNoAdmin(intUserID);
                    //drNguoiDungCapDuois = dsNguoiDungCapDuoi.Tables[0].Rows;
                    //foreach (DataRow drQuyen in dsQuyen.Tables[0].Rows)
                    //{
                    //    ComponentArt.Web.UI.TreeViewNode rootNodeQuyen = new ComponentArt.Web.UI.TreeViewNode();
                    //    rootNodeQuyen.Text = drQuyen["Tennhom"].ToString();
                    //    rootNodeQuyen.ImageUrl = "folder.gif";
                    //    rootNodeQuyen.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyen["ID"].ToString() + "&UserList=" + intUserID.ToString();
                    //    //rootNodeQuyen.ID = drQuyen["ID"].ToString();
                    //    this.tvLevel.Nodes.Add(rootNodeQuyen);

                    //    int i = 1;
                    //    foreach (DataRow dr in drNguoiDungCapDuois)
                    //    {
                    //        int intNguoiDungCapDuoi = Convert.ToInt32(dr["ID"].ToString());
                    //        int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDungCapDuoi, 0);
                    //        ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                    //        rootNode.Text = dr["HoTen"].ToString() + " (" + intCount.ToString() + ")";
                    //        rootNode.ImageUrl = "user.gif";
                    //        rootNode.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyen["ID"].ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                    //       // rootNode.ID = intNguoiDungCapDuoi.ToString();
                    //        rootNodeQuyen.Nodes.Add(rootNode);
                    //        //if (intUserList == intNguoiDungCapDuoi)
                    //        //{
                    //        //    rootNode.Expanded = true;
                    //        //    this.tvLevel.SelectedNode = tvLevel.Nodes[i];
                    //        //}
                    //        i++;
                    //        DataSet dsNguoiDungCapDuoiLevel = objNguoidung.loadNguoiDungLevel(intNguoiDungCapDuoi);
                    //        if (dsNguoiDungCapDuoiLevel != null && dsNguoiDungCapDuoiLevel.Tables[0].Rows.Count > 0)
                    //        {
                    //            BuildTreeSub(dsNguoiDungCapDuoiLevel.Tables[0].Rows, rootNode, intNguoiDungCapDuoi, Convert.ToInt32(drQuyen["ID"].ToString()));
                    //        }
                    //     }
                    //  }
                }
                else
                {

                    DataSet dsQuyen = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
                    DataRowCollection drQuyens = dsQuyen.Tables[0].Rows;
                    DataSet dsNguoiDungCapDuoi = objNguoidung.loadQuyenNguoiDungCapDuoiByNguoiDung(intUserID);
                    drNguoiDungCapDuois = dsNguoiDungCapDuoi.Tables[0].Rows;
                    if (drNguoiDungCapDuois.Count > 0)
                    {
                        foreach (DataRow drQuyen in drQuyens)
                        {
                            ComponentArt.Web.UI.TreeViewNode rootNodeQuyen = new ComponentArt.Web.UI.TreeViewNode();
                            DataRow drQuyenTemp = objQuyen.loadID(Convert.ToInt32(drQuyen["QuyenID"].ToString()));
                            rootNodeQuyen.Text = drQuyenTemp["Tennhom"].ToString();
                            rootNodeQuyen.ImageUrl = "folder.gif";
                            rootNodeQuyen.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyenTemp["ID"].ToString() + "&UserList=" + intUserID.ToString();
                            // rootNodeQuyen.ID = drQuyen["QuyenID"].ToString();
                            this.tvLevel.Nodes.Add(rootNodeQuyen);


                            int i = 1;
                            foreach (DataRow dr in drNguoiDungCapDuois)
                            {
                                int intNguoiDungCapDuoi = Convert.ToInt32(dr["ID"].ToString());
                                int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDungCapDuoi, 0);
                                ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                                rootNode.Text = dr["HoTen"].ToString() + " (" + intCount.ToString() + ")";
                                rootNode.ImageUrl = "user.gif";
                                rootNode.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + drQuyenTemp["ID"].ToString() + "&UserList=" + intNguoiDungCapDuoi.ToString();
                                //    rootNode.ID = intNguoiDungCapDuoi.ToString();
                                rootNodeQuyen.Nodes.Add(rootNode);
                                //if (intUserList == intNguoiDungCapDuoi)
                                //{
                                //    rootNode.Expanded = true;
                                //    this.tvLevel.SelectedNode = tvLevel.Nodes[i];
                                //}
                                i++;
                                DataSet dsNguoiDungCapDuoiLevel = objNguoidung.loadNguoiDungLevel(intNguoiDungCapDuoi);
                                if (dsNguoiDungCapDuoiLevel != null && dsNguoiDungCapDuoiLevel.Tables[0].Rows.Count > 0)
                                {
                                    BuildTreeSub(dsNguoiDungCapDuoiLevel.Tables[0].Rows, rootNode, intNguoiDungCapDuoi, Convert.ToInt32(drQuyen["QuyenID"].ToString()));
                                }
                            }
                        }
                    }
                   
                }

            }
        }

        catch
        { }

    }
    private void BuildTreeSub1(DataRowCollection drLevels, ComponentArt.Web.UI.TreeViewNode parentNode, int intNguoiDung)
    {
        foreach (DataRow dr in drLevels)
        {
            int LevelID = Convert.ToInt32(dr["ID"].ToString());
            int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDung, LevelID);
            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = dr["Ten"].ToString() + " (" + intCount.ToString() + ")";
            if (intLevelID == LevelID && intUserList == intNguoiDung)
            {
                node.CssClass = "SelectedTreeNode";
            }
            node.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&LevelView=" + dr["ID"].ToString() + "&UserList=" + intNguoiDung.ToString();
            node.ID = dr["Ma"].ToString();
            node.ImageUrl = "file.gif";
            parentNode.Nodes.Add(node);
        }
    }   
    // Builds a TreeView level containing the list of sub-folders of the given directory 
    private void BuildTreeSub(DataRowCollection drLevels, ComponentArt.Web.UI.TreeViewNode parentNode, int intNguoiDung,int NhomID)
    {
        foreach (DataRow dr in drLevels)
        {
            int LevelID = Convert.ToInt32(dr["ID"].ToString());
            int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiDung, LevelID);
            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = dr["Ten"].ToString() + " (" + intCount.ToString() + ")";
            if (intLevelID == LevelID && intUserList == intNguoiDung)
            {
                node.CssClass = "SelectedTreeNode";
            }
            node.NavigateUrl = "Default.aspx?ChucNang=" + intChucNangID.ToString() + "&Nhom=" + NhomID.ToString() + "&UserList=" + intNguoiDung.ToString() + "&LevelView=" + dr["ID"].ToString();
            //node.ID = dr["Ma"].ToString();
            node.ImageUrl = "file.gif";
            parentNode.Nodes.Add(node);
        }
    }   
    
    private void loadDemSoHocVienTheoThaiGoiByLevel(int LevelID,int intUserID)
    {
        try
        {
            DataSet dsTrangThai = objTrangThaiGoi.LayDanhSachTrangThaiGoi();
            DataSet dsTenTruong = objTruong.LayDanhSachTruong();
            this.rblTrangThaiGoi.Items.Clear();
            this.rblTenTruong.Items.Clear();
            ListItem liTemp = new ListItem();
            liTemp.Text = "Tất cả";
            liTemp.Value = "0";
            liTemp.Selected = true;
            this.rblTrangThaiGoi.Items.Add(liTemp);
            this.rblTenTruong.Items.Add(liTemp);

            DateTime v_dat_nhap_tu_ngay, v_dat_nhap_den_ngay, v_dat_goi_tu_ngay, v_dat_goi_den_ngay;
            if (this.m_dat_nhap_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001")) 
                v_dat_nhap_tu_ngay = this.m_dat_nhap_tu_ngay.SelectedDate;
            else v_dat_nhap_tu_ngay = Convert.ToDateTime("01/01/1900");
            
            if (this.m_dat_nhap_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_nhap_den_ngay = this.m_dat_nhap_den_ngay.SelectedDate;
            else v_dat_nhap_den_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_tu_ngay = this.m_dat_goi_tu_ngay.SelectedDate;
            else v_dat_goi_tu_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_den_ngay = this.m_dat_goi_den_ngay.SelectedDate;
            else v_dat_goi_den_ngay = Convert.ToDateTime("01/01/1900");
            
            string v_str_kieu_nhap_contact = m_cbo_kieu_nhap_contact.SelectedValue;
            foreach (DataRow dr in dsTrangThai.Tables[0].Rows)
            {
                int intCountTrangThai = objHocVien.loadDemSoHocVienTheoThaiGoiByLevelAndNgay(intUserID
                                                                                            , LevelID
                                                                                            , Convert.ToInt32(dr["ID"].ToString())
                                                                                            , v_dat_nhap_tu_ngay
                                                                                            , v_dat_nhap_den_ngay
                                                                                            , v_dat_goi_tu_ngay
                                                                                            , v_dat_goi_den_ngay
                                                                                            , v_str_kieu_nhap_contact);
                liTemp = new ListItem();
                liTemp.Text = dr["Ten"].ToString() + "(" + intCountTrangThai.ToString() + ")";
                liTemp.Value = dr["ID"].ToString();
                this.rblTrangThaiGoi.Items.Add(liTemp);
            }
            foreach (DataRow dr in dsTenTruong.Tables[0].Rows)
            {
                int intCountTenTruong = objHocVien.loadDemSoHocVienTheoTruongByLevel(intUserID
                                                                                    , LevelID
                                                                                    , Convert.ToInt32(dr["ID"].ToString())
                                                                                    , v_dat_nhap_tu_ngay
                                                                                    , v_dat_nhap_den_ngay
                                                                                    , v_dat_goi_tu_ngay
                                                                                    , v_dat_goi_den_ngay
                                                                                    , v_str_kieu_nhap_contact);
                liTemp = new ListItem();
                liTemp.Text = dr["Ten"].ToString() + "(" + intCountTenTruong.ToString() + ")";
                liTemp.Value = dr["ID"].ToString();
                this.rblTenTruong.Items.Add(liTemp);
            }
            if (m_hdf_trangthaiID.Value != "") rblTrangThaiGoi.SelectedValue = m_hdf_trangthaiID.Value;
        }
        catch
        { }
    }

    /// <summary>
    ///  /// Created by LinhDH
    /// Hàm này dùng để kiểm tra người dùng đăng nhập. 
    /// Nếu nằm trong danh sách cấm, không được xuất excel thì invisible nút xuất excel
    /// </summary>
    /// <returns>true nếu người dùng bị cấm; false nếu ng dùng ko bị cấm</returns>
    private bool check_quyen_xuat_excel()
    {
        string v_str_duong_dan_file;
        int v_i_flag = 0;
        v_str_duong_dan_file = Server.MapPath("~/UserControl/HocVien/DS_tai_khoan_dc_xuat_contact.txt");
        string[] v_str_danh_sach_nguoi_dung_cam = File.ReadAllLines(v_str_duong_dan_file);
        string v_str_account = Session["Username"].ToString().ToLower();
        for (int v_i = 0; v_i < v_str_danh_sach_nguoi_dung_cam.Length; v_i++)
        {
            if (v_str_danh_sach_nguoi_dung_cam[v_i].ToLower().Equals(v_str_account))
            {
                v_i_flag = 1;
                break;
            }
        }
        if (v_i_flag > 0) return false;  // Account đc xuất
        return true; // User bị cấm
    }
    
    /// <summary>
    /// Hàm này kiểm tra tài khoản trong file text; nếu có trong Ds đó thì được quyền xóa cts (đối với quyền QuanLy ko xuất data)
    /// </summary>
    /// <returns></returns>
    private bool check_quyen_xoa_umg_vien()
    {
        string v_str_duong_dan_file;
        int v_i_flag = 0;
        v_str_duong_dan_file = Server.MapPath("~/UserControl/HocVien/DS_tai_khoan_duoc_xoa_cts_quan_ly_ko_xuat_data.txt");
        string[] v_str_danh_sach_nguoi_dung_duoc_xoa = File.ReadAllLines(v_str_duong_dan_file);
        // Account hiện đang đăng nhập
        string v_str_account = Session["Username"].ToString();
        for (int v_i = 0; v_i < v_str_danh_sach_nguoi_dung_duoc_xoa.Length; v_i++)
        {
            if (v_str_danh_sach_nguoi_dung_duoc_xoa[v_i].Equals(v_str_account))
            {
                v_i_flag = 1;  // nghĩa là được xóa
                break;
            }
        }
        if (v_i_flag > 0) return true;  // Account được xóa
        return false; // User ko được xóa
    }

    //PhiNV
    public string getTenTruong(string truongID)
    {
        int i_truong = 0;
        int.TryParse(truongID, out i_truong);
        if (i_truong != 0)
        {
          DataRow row =  objTruong.loadTruongID(i_truong);
          if (row != null)
          {
              return (row[1] != null) ? row[1].ToString() : "---";
          }
          else return "---";
        }
        else return "---";
    }
    private void loadTimDanhSachHocVien()
    {
        try
        {
            string HoTen = this.txtTenHocVien.Text.Trim();
            string MaHocVien = this.txtMaHocVien.Text.Trim();
            string DienThoai = Utilities.chuan_hoa_so_dien_thoai(this.txtDienThoai.Text.Trim());
            string Email = this.txtEmail.Text.Trim();
            DateTime v_dat_nhap_tu_ngay, v_dat_nhap_den_ngay, v_dat_goi_tu_ngay, v_dat_goi_den_ngay;

            if (this.m_dat_nhap_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_nhap_tu_ngay = this.m_dat_nhap_tu_ngay.SelectedDate;
            else v_dat_nhap_tu_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_nhap_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_nhap_den_ngay = this.m_dat_nhap_den_ngay.SelectedDate;
            else v_dat_nhap_den_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_tu_ngay = this.m_dat_goi_tu_ngay.SelectedDate;
            else v_dat_goi_tu_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_den_ngay = this.m_dat_goi_den_ngay.SelectedDate;
            else v_dat_goi_den_ngay = Convert.ToDateTime("01/01/1900");
            string v_str_kieu_nhap_contact;

            v_str_kieu_nhap_contact = m_cbo_kieu_nhap_contact.SelectedValue;

            int TrangThaiGoiID = 0;
            int TruongID = 0;
            bool boolCheck = StringUntil.checkNumber(this.rblTrangThaiGoi.SelectedValue);
            bool boolChecks = StringUntil.checkNumber(this.rblTenTruong.SelectedValue);
            if (boolCheck)
            {
                TrangThaiGoiID = Convert.ToInt32(this.rblTrangThaiGoi.SelectedValue);
            }
            if (boolChecks)
            {
                TruongID = Convert.ToInt32(this.rblTenTruong.SelectedValue);
            }
            DataView dv = new DataView();
            DataSet dsHocVien = objHocVien.searchTimDanhSachHocVien(intUserList, intLevelID
                                                                    , HoTen, MaHocVien
                                                                    , DienThoai, Email
                                                                    , TrangThaiGoiID, TruongID
                                                                    , v_dat_nhap_tu_ngay
                                                                    , v_dat_nhap_den_ngay
                                                                    , v_dat_goi_tu_ngay
                                                                    , v_dat_goi_den_ngay
                                                                    , v_str_kieu_nhap_contact);
            if (ViewState["sortExpr"] != null)
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
                dv.Sort = (string)ViewState["sortExpr"];
            }
            else
            {
                dv = new DataView(DateTimeUntil.ConvertTable(dsHocVien.Tables[0], false));
            }
            this.grvHocVien.DataSource = dv;
            this.grvHocVien.DataBind();
            //ViewState["theDataTable"] = grvHocVien.DataSource;
            m_lbl_result.Text = "Kết quả lọc: " + dsHocVien.Tables[0].Rows.Count +" ứng viên";
        }
        catch
        { }
    }

    // Xoa NguoiTimNguon
    private void DeleteNguoiTimNguon(object source, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int intHocVienID = Convert.ToInt32(grvHocVien.DataKeys[e.RowIndex].Value);
        int intChẹck = objHocVien.removeHocVien(intHocVienID, intUserID);
        if (intChẹck > 0)
        {
            this.lblMess.Text = "Xóa thành công bản ghi";
            Session["UserID"] = intUserID;
            objLog.insertLog(0, intHocVienID, 16, intUserID);
            grvHocVien.EditIndex = -1;
            loadTimDanhSachHocVien();
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
        }
    }
    public void SaveExcel(string strNamFile)
    {
        //DataTable dt = ((DataView)grvHocVien.DataSource).Table;
        //DataTable dt = ((DataView)ViewState["theDataTable"]).Table;
        string HoTen = this.txtTenHocVien.Text.Trim();
        string MaHocVien = this.txtMaHocVien.Text.Trim();
        string DienThoai = this.txtDienThoai.Text.Trim();
        string Email = this.txtEmail.Text.Trim();

        string v_str_kieu_nhap_contact = m_cbo_kieu_nhap_contact.SelectedValue;

        DateTime v_dat_nhap_tu_ngay, v_dat_nhap_den_ngay, v_dat_goi_tu_ngay, v_dat_goi_den_ngay;

        if (this.m_dat_nhap_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
            v_dat_nhap_tu_ngay = this.m_dat_nhap_tu_ngay.SelectedDate;
        else v_dat_nhap_tu_ngay = Convert.ToDateTime("01/01/1900");

        if (this.m_dat_nhap_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
            v_dat_nhap_den_ngay = this.m_dat_nhap_den_ngay.SelectedDate;
        else v_dat_nhap_den_ngay = Convert.ToDateTime("01/01/1900");

        if (this.m_dat_goi_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
            v_dat_goi_tu_ngay = this.m_dat_goi_tu_ngay.SelectedDate;
        else v_dat_goi_tu_ngay = Convert.ToDateTime("01/01/1900");

        if (this.m_dat_goi_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
            v_dat_goi_den_ngay = this.m_dat_goi_den_ngay.SelectedDate;
        else v_dat_goi_den_ngay = Convert.ToDateTime("01/01/1900");
        int TrangThaiGoiID = 0;
        int TruongID = 0;
        bool boolCheck = StringUntil.checkNumber(this.rblTrangThaiGoi.SelectedValue);
        bool boolChecks = StringUntil.checkNumber(this.rblTenTruong.SelectedValue);
        if (boolCheck)
        {
            TrangThaiGoiID = Convert.ToInt32(this.rblTrangThaiGoi.SelectedValue);
        }
        if (boolChecks)
        {
            TruongID = Convert.ToInt32(this.rblTenTruong.SelectedValue);
        }
        DataView dv = new DataView();
        DataSet dsHocVien = objHocVien.searchTimDanhSachHocVien(intUserList
                                                                   , intLevelID
                                                                   , HoTen
                                                                   , MaHocVien
                                                                   , DienThoai
                                                                   , Email
                                                                   , TrangThaiGoiID
                                                                   , TruongID
                                                                   , v_dat_nhap_tu_ngay
                                                                   , v_dat_nhap_den_ngay
                                                                   , v_dat_goi_tu_ngay
                                                                   , v_dat_goi_den_ngay
                                                                   , v_str_kieu_nhap_contact);
        DataTable dt = dsHocVien.Tables[0];
        dt.Columns.Add("NoidungCuocGoi");        
        for (int i =0; i < dt.Rows.Count ; i++)
        {
            int intHocVienID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
            int intLevel_ID = Convert.ToInt32(dt.Rows[i]["LevelID"].ToString());
            if (intLevel_ID == 2)
            {

                DataRow dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevel_ID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevel_ID > 2 && intLevel_ID < 7)
            {
                DataRow dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevel_ID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else if (intLevel_ID > 6 && intLevel_ID < 10)
            {
                DataRow dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevel_ID);
                dt.Rows[i]["NoidungCuocGoi"] = dr["NoiDungNgayGoi"].ToString();
            }
            else
            {
                dt.Rows[i]["NoidungCuocGoi"] = "";
            }
        }

        string html = loadReportExportExcel(dt);
		Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
        Response.Clear();
        Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        Response.Write("\r\n");
        Response.Write(html);
        Response.End();
    }
    public string loadReportExportExcel(DataTable dt)
    {
        try
        {
            string strHTML = "";
            strHTML += "\n  <col span=8 style='width:100%;' border='1'>"; 
            strHTML += "\n      <tr>";
            strHTML += "\n          <td class=xl23 colspan=8>Danh Sách Ứng Viên</td>";
            strHTML += "\n      </tr>        ";
            strHTML += "\n      <tr > ";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\" >TT</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Mã ứng viên</td>";
            strHTML += "\n          <td class=xl24>Họ tên</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Điện thoại</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Email</td>";
            strHTML += "\n          <td class=xl24 style=\"width:8%;\">Trạng thái</td>";
            strHTML += "\n          <td class=xl24>Nội dung</td>";
            strHTML += "\n          <td class=xl24>Trường tốt nghiệp</td>";
            strHTML += "\n          <td class=xl24>Ngành đăng ký học</td>";
            strHTML += "\n          <td class=xl24>Level</td>";
            strHTML += "\n          <td class=xl24>Ngày gọi gần nhất</td>";
            strHTML += "\n          <td class=xl24>TVTS</td>";
            //strHTML += "\n          <td class=xl24>Tên Trường</td>";
            strHTML += "\n      </tr>";
            foreach (DataRow dr in dt.Rows)
            {
                strHTML += "\n  <tr >";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["Seq"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["MaHocVien"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["HoTen"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["DienThoai"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["Email"].ToString() + "</td>";
                
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["TenTrangThaiGoi"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["NoidungCuocGoi"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25  ><span class=\"cssLabel\">" + dr["DoiTuong"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25   ><span class=\"cssLabel\">" + dr["Nghanh"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["Level"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["NgayGoi"].ToString() + "</td>";
                strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + Utilities.GetFullNameTVTS(dr["NguoiDungID"].ToString()) + "</td>";
                //strHTML += "\n      <td class=xl25 ><span class=\"cssLabel\">" + dr["TenTruong"].ToString() + "</td>";
                strHTML += "\n  </tr>";
            }
            strHTML = FileUntil.strFileExcel(strHTML);
            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    #endregion
    
    #region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objHocVienLV2 = new DAHocVienLV2(sqlConnec);
        objHocVienLV3LV5 = new DAHocVienLV3LV5(sqlConnec);
        objHocVienLV6LV8 = new DAHocVienLV6LV8(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objTrangThaiGoi = new DATrangThaiGoi(sqlConnec);
        objTrangThai = new DATrangThai(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objQuyen = new DAQuyen(sqlConnec);
        objLog = new DALog(sqlConnec);
        objTruong = new DATruong(sqlConnec);
    }
    //Main load
    private bool checkQuyenUser(int UserID)
    {
        if (intUserID == 1 || check_quyen_xoa_umg_vien())
        {
            return true;
        }
        else
        {
            DataSet dsNguoidung = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
            if (dsNguoidung != null && dsNguoidung.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsNguoidung.Tables[0].Rows)
                {
                    if (dr["QuyenID"].ToString().Equals("1") || dr["QuyenID"].ToString().Equals("2"))
                    {
                        return true;
                        
                    }
                }
            }
        }
        return false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        loadDataAccess();
        m_lbl_result.Text = "";
        intUserID = Convert.ToInt32(Session["UserID"].ToString());
        if (Request.QueryString["ChucNang"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["ChucNang"].Trim());
            if (boolCheck)
            {
                intChucNangID = Convert.ToInt32(Request.QueryString["ChucNang"].Trim());
            }
        }
        if (Request.QueryString["Nhom"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["Nhom"].Trim());
            if (boolCheck)
            {
                intNhom = Convert.ToInt32(Request.QueryString["Nhom"].Trim());
            }
        }
        if (Request.QueryString["LevelView"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["LevelView"].Trim());
            if (boolCheck)
            {
                intLevelID = Convert.ToInt32(Request.QueryString["LevelView"].Trim());
            }
        }
        if (Request.QueryString["UserList"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["UserList"].Trim());
            if (boolCheck)
            {
                intUserList = Convert.ToInt32(Request.QueryString["UserList"].Trim());
            }
        }
        if (!Page.IsPostBack)
        {
            try
            {
                loadDemSoHocVienTheoThaiGoiByLevel(intLevelID, intUserList);
                loadLevel();
                loadTimDanhSachHocVien();
                bool checkThemungvien = false;

                if (intUserID == 1)
                {
                    checkThemungvien = true;
                }
                else
                {
                    DataSet dsNguoidung = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
                    if (dsNguoidung != null && dsNguoidung.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in dsNguoidung.Tables[0].Rows)
                        {
                            //if (dr["QuyenID"].ToString().Equals("1") || dr["QuyenID"].ToString().Equals("2") || dr["QuyenID"].ToString().Equals("4") || dr["QuyenID"].ToString().Equals("5"))
                            //{
                                checkThemungvien = true;
                                break;
                            //}
                        }
                    }
                }
                if (checkThemungvien)
                {
                    this.btnThemHocVien.Visible = true;
                    this.btnThemHocVien.Attributes.Add("OnClick", "JavaScript:OpenWinCenter2(350, 800, '', 'EventHocVien.aspx?LevelView=" + intLevelID + "&TaoHocVien=True');");
                }
                else
                {
                    this.btnThemHocVien.Visible = false;
                }
                // nếu account được dùng chức năng xuất excel
                if (!check_quyen_xuat_excel())
                {
                    btnInDanhSach.Visible = true;
                }
                else
                {
                    btnInDanhSach.Visible = false;
                    btnInDanhSach.Enabled = false;
                }
            }
            catch
            { }
        }
    }

    //Su kien bin from NguoiTimNguon
    protected void grvHocVien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                //e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                //e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
                // Modify     
                Label HocVienIDTemp = ((Label)e.Row.FindControl("HocVienID"));
                Label LevelIDTemp = ((Label)e.Row.FindControl("LevelID"));
                ImageButton lbtDeleteTemp = ((ImageButton)e.Row.FindControl("btnDelete"));
                Label lblMaHocVienGVTemp = ((Label)e.Row.FindControl("lblMaHocVienGV"));
                HyperLink hplUpdateGVTemp = ((HyperLink)e.Row.FindControl("hplUpdateGV"));
                HyperLink hplAddGVTemp = ((HyperLink)e.Row.FindControl("hplAddGV"));
                if (lblMaHocVienGVTemp != null)
                {
                    string strViewMaHocVien = "JavaScript:OpenWinCenter(700, 800, '" + lblMaHocVienGVTemp.Text + "', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');";
                    e.Row.Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[2].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[3].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[4].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[5].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[6].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[7].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[8].Attributes["Ondblclick"] = strViewMaHocVien;
                    //e.Row.Cells[9].Attributes["Ondblclick"] = strViewMaHocVien;
                }
                hplAddGVTemp.Attributes.Add("OnClick", "JavaScript:OpenWinCenter(700, 800, '', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&UserList=" + intUserList.ToString() + "&HocVienID=" + HocVienIDTemp.Text + "&EventCreate=True');");
                hplUpdateGVTemp.Attributes.Add("OnClick", "JavaScript:OpenWinCenter2(350, 800, '', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&TaoHocVien=True&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');");
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien();
        loadDemSoHocVienTheoThaiGoiByLevel(intLevelID, intUserList);
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.txtTenHocVien.Text = "";
        this.txtMaHocVien.Text = "";
        this.txtDienThoai.Text = "";
        this.txtEmail.Text = "";
        loadTimDanhSachHocVien();
    }
    protected void btnInDanhSach_Click(object sender, EventArgs e)
    {
        SaveExcel("Danhsachungvien" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xls");
    }
    //Su kien xoa NguoiTimNguon
    protected void grvHocVien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        this.lblMess.Text = "";
        DeleteNguoiTimNguon(sender, e);
    }
    protected void grvHocVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvHocVien.EditIndex = -1;
        this.grvHocVien.PageIndex = e.NewPageIndex;
        loadTimDanhSachHocVien();
    }
    protected void rblTrangThaiGoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        m_hdf_trangthaiID.Value = rblTrangThaiGoi.SelectedValue;
        loadTimDanhSachHocVien();
        //if (Request.QueryString["LevelView"] != null)
        //{
        //    bool boolCheck = StringUntil.checkNumber(Request.QueryString["LevelView"].Trim());
        //    if (boolCheck)
        //    {
        //        intLevelID = Convert.ToInt32(Request.QueryString["LevelView"].Trim());
        //    }
        //}
        //if (Request.QueryString["UserList"] != null)
        //{
        //    bool boolCheck = StringUntil.checkNumber(Request.QueryString["UserList"].Trim());
        //    if (boolCheck)
        //    {
        //        intUserList = Convert.ToInt32(Request.QueryString["UserList"].Trim());
        //    }
        //}
        loadDemSoHocVienTheoThaiGoiByLevel(intLevelID, intUserList);
        m_hdf_trangthaiID.Value = "";
    }
    protected void rblTenTruong_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien();
    }
    protected void grvHocVien_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortOrder"] == null)
        {
            ViewState["SortOrder"] = " ASC";
        }
        else if (ViewState["SortOrder"].ToString() == " ASC")
        {
            ViewState["SortOrder"] = " DESC";
        }
        else
        {
            ViewState["SortOrder"] = " ASC";
        }
        ViewState["sortExpr"] = e.SortExpression + ViewState["SortOrder"];
        loadTimDanhSachHocVien();
    }
#endregion    
}
